package com.ptszyxx.popose.module.main.swipe.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ptszyxx.popose.R;
import com.ysg.http.data.entity.home.HomeResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YImageUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class SwipeAdapter extends BaseAdapter {
    private List<HomeResult> list = new ArrayList<>();

    public void addAll(Collection<HomeResult> collection) {
        if (YCollectionUtil.isNotEmpty(collection)){
            list.addAll(collection);
            notifyDataSetChanged();
        }
    }
    public void remove(int index) {
        if (index > -1 && list != null && index < list.size()) {
            list.remove(index);
            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public HomeResult getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        HomeResult item = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_swipe, parent, false);
            holder  = new ViewHolder();
            convertView.setTag(holder);
            holder.ivAvatar = (ImageView) convertView.findViewById(R.id.ivAvatar);
            holder.ivClose = (ImageView) convertView.findViewById(R.id.ivClose);
            holder.ivMessage = (ImageView) convertView.findViewById(R.id.ivMessage);
            holder.ivLike = (ImageView) convertView.findViewById(R.id.ivLike);
            holder.tvNick = (TextView) convertView.findViewById(R.id.tvNick);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        YImageUtil.show(holder.ivAvatar, item.getUsericon());
        holder.tvNick.setText(item.getUsernick());

        holder.ivClose.setOnClickListener((view)->{
            if (listener != null) listener.onCloseClick(item);
        });
        holder.ivMessage.setOnClickListener((view)->{
            if (listener != null) listener.onMessageClick(item);
        });
        holder.ivLike.setOnClickListener((view)->{
            if (listener != null) listener.onLikeClick(item);
        });

        return convertView;
    }

    private static class ViewHolder {
        ImageView ivAvatar;
        ImageView ivClose;
        ImageView ivMessage;
        ImageView ivLike;
        TextView tvNick;
    }

    public interface OnClickListener{
        void onCloseClick(HomeResult item);
        void onMessageClick(HomeResult item);
        void onLikeClick(HomeResult item);
    }
    private OnClickListener listener;
    public void setOnItemClickListener(OnClickListener listener){
        this.listener = listener;
    }
}
